<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/22
 * Time: 10:26
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Bigcommerce_api
{
    private $Client = '';
    private $Token = '';
    private $stores = '';

    public function __construct($bigcom = array())
    {

        $this->Client = $bigcom['bigcom_client'];
        $this->Token = $bigcom['bigcom_token'];
        $this->stores = $bigcom['bigcom_stores'];
        return $this;
    }

    //添加商品
    public function Bigcommerce_add_goods($goods_arr){
        $url = 'https://api.bigcommerce.com/stores/'.$this->stores.'/v3/catalog/products';
        $goods_arr = json_encode($goods_arr);
        return $this->bigcommerce_url($goods_arr,$url,'POST');
    }

    //添加sku
    public function Bigcommerce_add_sku($goods_arr,$product_id){
        $url = 'https://api.bigcommerce.com/stores/'.$this->stores.'/v3/catalog/products/'.$product_id.'/options';
        $goods_arr = json_encode($goods_arr);
        return $this->bigcommerce_url($goods_arr,$url,'POST');
    }



    public function bigcommerce_url($data_string,$url,$method = 'POST'){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $headers = array(
            "Content-Type: application/json",
            "Accept: application/json",
            "X-Auth-Client: ".$this->Client,
            "X-Auth-Token: ".$this->Token,
        );
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $content = curl_exec($ch);
        curl_close($ch);
        return json_decode($content, true);
    }


    
}

